<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="生产订单编号">
          <el-input
            v-model="searchParam.produceOrderCode"
            placeholder="请输入生产订单编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantCode"
            clearable
            placeholder="请选择工厂"
          >
            <el-option
              v-for="item in plants"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.organizationCode"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="组织机构">
          <el-select
            v-model="searchParam.organizationCode"
            clearable
            placeholder="请选择组织机构"
          >
            <el-option
              v-for="item in organizations"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.organizationCode"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="工作中心">
          <el-input
            v-model="searchParam.workCenterCode"
            placeholder="请输入工作中心编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <div v-if="isExpanded">
          <el-form-item label="存货编码">
            <el-input
              v-model="searchParam.materialCode"
              placeholder="请输入存货编码"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>

          <el-form-item label="订单来源">
            <el-select
              v-model="searchParam.produceOrderSource"
              placeholder="请选择订单来源"
              clearable
            >
              <el-option
                v-for="item in useDictionary('ProduceOrderSource')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="生产计划编号">
            <el-input
              v-model="searchParam.producePlanCode"
              placeholder="请输入生产计划编号"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="父生产订单号">
            <el-input
              v-model="searchParam.parentProduceOrderId"
              placeholder="请输入父生产订单id"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>

          <el-form-item label="计划批次">
            <el-input
              v-model="searchParam.planBatch"
              placeholder="请输入计划批次"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="班制">
            <el-select
              v-model="searchParam.shiftSystemId"
              clearable
              placeholder="请选择班制"
            >
              <el-option
                v-for="item in shiftsets"
                :key="item.id"
                :label="item.shiftSetName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="班次">
            <el-select
              v-model="searchParam.shiftId"
              clearable
              placeholder="请选择班次"
            >
              <el-option
                v-for="item in shifts"
                :key="item.id"
                :label="item.shiftName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="计划状态">
            <el-select
              v-model="searchParam.orderState"
              placeholder="请选择计划状态"
              clearable
            >
              <el-option
                v-for="item in useDictionary('ProduceOrderState')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="计划日期">
            <el-date-picker
              v-model="searchParam.planDateSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getplanDate"
            />
          </el-form-item>
        </div>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
          <el-button
            class="ml-3"
            @click="
              useDownload(
                '/mes/mesproduceorder/export',
                '导出表格名称',
                searchParam
              )
            "
          >
            <template #icon><i-ep-download /></template>导出</el-button
          >
          <el-button type="primary" @click="toggleSearchArea" link>
            {{ isExpanded ? "收起" : "展开" }}
            <el-icon v-if="isExpanded" class="el-icon--right">
              <i-ep-arrowUp color="#409EFC" />
            </el-icon>
            <el-icon v-else class="el-icon--right">
              <i-ep-arrowDown color="#409EFC" />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" type="index" width="55" align="center" />
        <el-table-column
          label="生产订单编号"
          prop="produceOrderCode"
          min-width="120"
          show-overflow-tooltip
        />

        <el-table-column
          label="工厂"
          prop="plantCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="组织机构"
          prop="organizationCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作中心"
          prop="workCenterCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="存货编码"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划数量"
          prop="planQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="完成数量"
          prop="planFinishQuantity"
          min-width="100"
          show-overflow-tooltip
          header-align="center"
          align="right"
        />
        <el-table-column
          label="订单来源"
          prop="produceOrderSource"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{
              useDictionary("ProduceOrderSource", scope.row.produceOrderSource)
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="生产计划编号"
          prop="producePlanCode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="父生产订单号"
          prop="parentProduceOrderId"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="计划日期"
          prop="planDate"
          min-width="180"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划批次"
          prop="planBatch"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="班制"
          prop="shiftSystemId"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="班次"
          prop="shiftId"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column
          label="计划状态"
          prop="orderState"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("ProduceOrderState", scope.row.orderState) }}
          </template>
        </el-table-column>
        <!-- <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-edit />
              详情
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesproduceorder-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESMESPRODUCEORDER",
  inheritAttrs: false,
});
let plants = ref([]);
let lines = ref([]);
let shifts = ref([]);
let shiftsets = ref([]);
let organizations = ref([]);
// 请求参数
const requestApi = "/mes/mesproduceorder/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  produceOrderCode: "",
  organizationCode: "",
  plantCode: "",
  workCenterCode: "",
  planDateSearch: [],
  planDateStart: "",
  planDateEnd: "",
  shiftSystemId: "",
  shiftId: "",
  materialCode: "",
  planBatch: "",
  produceOrderSource: "",
  producePlanCode: "",
  parentProduceOrderId: "",
  orderState: "",
};

const deleteParam = {
  api: "/mes/mesproduceorder/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}
const isExpanded = ref(false);
const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlants();
  getLines();
  getShifts();
  getShiftsets();
  getorganizations();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  const { data } = await request({
    url: `/mes/mesproduceorder/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
}
async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
  searchParam.value.plantCode = plants.value[0].organizationCode;
}
async function getorganizations() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 2 },
  });
  organizations.value = data.data;
}
async function getLines() {
  const data = await request({
    url: "/mdm/produceline/getList",
    method: "get",
    params: {},
  });
  lines.value = data.data;
}
async function getShifts() {
  const data = await request({
    url: "/mes/messhift/getList",
    method: "post",
    data: {},
  });
  shifts.value = data.data;
}
async function getShiftsets() {
  const data = await request({
    url: "/mes/messhiftset/getList",
    method: "post",
    data: {},
  });
  shiftsets.value = data.data;
}
/** 打开生产订单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "生产订单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
function toggleSearchArea() {
  isExpanded.value = !isExpanded.value;
}
function getplanDate(data: any) {
  if (data) {
    searchParam.value.planDateStart = data[0];
    searchParam.value.planDateEnd = data[1];
  } else {
    searchParam.value.planDateStart = "";
    searchParam.value.planDateEnd = "";
  }
}
</script>
